package com.xunyin.modules.user.rest;

import cn.hutool.core.util.StrUtil;
import com.google.common.collect.Maps;
import com.xunyin.api.ApiResult;
import com.xunyin.api.XunyinException;
import com.xunyin.common.bean.LocalUser;
import com.xunyin.common.interceptor.AuthCheck;
import com.xunyin.constant.SystemConfigConstants;
import com.xunyin.enums.UserCouponStatusEnum;
import com.xunyin.modules.couponnew.domain.MtUserCoupon;
import com.xunyin.modules.couponnew.service.UserCouponService;
import com.xunyin.modules.log.AppLog;
import com.xunyin.modules.services.CreatShareProductService;
import com.xunyin.modules.shop.service.YxSystemConfigService;
import com.xunyin.modules.user.domain.YxUser;
import com.xunyin.modules.user.param.PromParam;
import com.xunyin.modules.user.service.YxUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.*;

/**
 * @ClassName UserBillController
 * @Author xunyinyunshang  <3594419785@qq.com>
 * @Date 2024/5/8
 **/
@Slf4j
@RestController
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
@Api(value = "用户分销", tags = "用户:用户分销")
public class UserBillController {

    private final YxSystemConfigService systemConfigService;

    private final CreatShareProductService creatShareProductService;

    private final YxUserService yxUserService;

    private final UserCouponService userCouponService;

    @Value("${file.path}")
    private String path;

    /**
     * 推广数据    昨天的佣金   累计提现金额  当前佣金
     */
    @AppLog(value = "查看推广数据", type = 1)
    @AuthCheck
    @GetMapping("/commission")
    @ApiOperation(value = "推广数据",notes = "推广数据")
    public ApiResult<Map<String,Object>> commission(){
        YxUser yxUser = LocalUser.getUser();

/*        //昨天的佣金
        double lastDayCount = userBillService.yesterdayCommissionSum(yxUser.getUid());
        //累计提现金额
        double extractCount = extractService.extractSum(yxUser.getUid());

        Map<String,Object> map = Maps.newHashMap();
        map.put("lastDayCount",lastDayCount);
        map.put("extractCount",extractCount);
        map.put("commissionCount",yxUser.getBrokeragePrice());*/

        List<String> statusList = Arrays.asList(UserCouponStatusEnum.UNUSED.getKey(), UserCouponStatusEnum.USED.getKey(), UserCouponStatusEnum.EXPIRE.getKey());
        List<MtUserCoupon> mtUserCouponList = userCouponService.getUserCouponList(yxUser.getUid().intValue(),statusList,"A");

        // 累计获得时长券数
        Integer totalCouponCount = 0;
        if(mtUserCouponList!=null && mtUserCouponList.size()>0){
            totalCouponCount = mtUserCouponList.size();
        }

        // 当前可用时长券数
        Integer unusedCouponCount = 0;
        statusList = Arrays.asList(UserCouponStatusEnum.UNUSED.getKey());
        mtUserCouponList = userCouponService.getUserCouponList(yxUser.getUid().intValue(),statusList,"A");
        if(mtUserCouponList!=null && mtUserCouponList.size()>0){
            unusedCouponCount = mtUserCouponList.size();
        }

        // 已用时长券数
        Integer usedCouponCount = 0;
        statusList = Arrays.asList(UserCouponStatusEnum.USED.getKey());
        mtUserCouponList = userCouponService.getUserCouponList(yxUser.getUid().intValue(),statusList,"A");
        if(mtUserCouponList!=null && mtUserCouponList.size()>0){
            usedCouponCount = mtUserCouponList.size();
        }

        Map<String,Object> map = Maps.newHashMap();
        map.put("totalCouponCount",totalCouponCount);
        map.put("unusedCouponCount",unusedCouponCount);
        map.put("usedCouponCount",usedCouponCount);
        return ApiResult.ok(map);
    }

    /**
     * 分销二维码海报生成
     */
    @AppLog(value = "分销二维码海报生成", type = 1)
    @AuthCheck
    @GetMapping("/spread/banner")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "form", value = "来源", paramType = "query", dataType = "string")
    })
    @ApiOperation(value = "分销二维码海报生成",notes = "分销二维码海报生成")
    public ApiResult<List<Map<String,Object>>> spreadBanner(@RequestParam(value = "",required=false) String from){
        YxUser yxUser = LocalUser.getUser();
        String siteUrl = systemConfigService.getData(SystemConfigConstants.SITE_URL);
        if(StrUtil.isEmpty(siteUrl)){
            throw new XunyinException("未配置h5地址!");
        }
        String apiUrl = systemConfigService.getData(SystemConfigConstants.API_URL);
        if(StrUtil.isEmpty(apiUrl)){
            throw new XunyinException("未配置api地址!");
        }
        log.info("from=============================================================:"+from);
        String spreadUrl = creatShareProductService.getSpreadUrl(from,yxUser,siteUrl,apiUrl,path);

        List<Map<String,Object>> list = new ArrayList<>();

        Map<String,Object> map = Maps.newHashMap();
        map.put("id",1);
        map.put("pic","");
        map.put("title","分享海报");
        map.put("wap_poster",spreadUrl);
        list.add(map);
        return ApiResult.ok(list);
    }

    /**
     *  推广人统计
     */
    @AppLog(value = "查看推广人统计", type = 1)
    @AuthCheck
    @PostMapping("/spread/people")
    @ApiOperation(value = "推广人统计",notes = "推广人统计")
    public ApiResult<Map<String,Object>> spreadPeople(@Valid @RequestBody PromParam param){

        Long uid = LocalUser.getUser().getUid();
        Map<String,Object> map = new LinkedHashMap<>();
        map.put("list",yxUserService.getUserSpreadGrade(uid,param.getPage(),param.getLimit()
                ,param.getGrade(),param.getKeyword(),param.getSort()));

        Map<String,Integer> countMap = yxUserService.getSpreadCount(uid);
        map.put("total",countMap.get("first"));
        map.put("totalLevel",countMap.get("second"));
        return ApiResult.ok(map);
    }


}
